﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using Business;
namespace Presentation
{
    public partial class frmTheLoai : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public frmTheLoai()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable("ten");
        DataView dv = new DataView();       
        private void frmTheLoai_Load(object sender, EventArgs e)
        {
            LoadGridView();
        }

        private void frmThemTheLoai_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Dong the loai");
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            frmMain main = this.ParentForm as frmMain;
            this.Close();
           
            if (main!=null)
            { 
                main.tabMain.Tabs.Remove(main.tabMain.SelectedTab);
            }
        }

       

        private void txtTimKiem_KeyPress(object sender, KeyPressEventArgs e)
        {
         //   MessageBox.Show((char.IsPunctuation(e.KeyChar)).ToString());

            if (char.IsPunctuation(e.KeyChar))
            {
                e.Handled = true;                
            }
           
            
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            XuLy();
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            dv.RowFilter = "TenTheLoai like '%" + txtTimKiem.Text + "%'";
            dgvTheLoai.DataSource = dv;
            if (txtTimKiem.Text == "")
            {
                //dv.RowStateFilter = DataViewRowState.OriginalRows;
                dgvTheLoai.DataSource = dt;
            }
        }

        private void dgvTheLoai_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {


        }
        private void XuLy()
        {
            dv.RowStateFilter = DataViewRowState.Added;
            DataTable dtThem = dv.ToTable();

            dv.RowStateFilter = DataViewRowState.ModifiedOriginal;
            DataTable dtCapNhat = dv.ToTable();

            dv.RowStateFilter = DataViewRowState.Deleted;
            DataTable dtXoa = dv.ToTable();
            string s, s1, s2;
            s = "Them";
            s1 = "Xoa";
            s2 = "Sua";
            for (int i = 0; i < dtThem.Rows.Count;i++ )
            {
                TheLoaiDTO tl = new TheLoaiDTO();
                tl.MaTheLoai = (int)dtThem.Rows[i]["MaTheLoai"];
                tl.TenTheLoai = (string)dtThem.Rows[i]["TenTheLoai"];
                TheLoaiService.Insert(tl);
            }
            for (int i = 0; i < dtXoa.Rows.Count;i++ )
            {
                int ma= (int)dtXoa.Rows[i]["MaTheLoai"];
                
            }
            for (int i = 0; i < dtCapNhat.Rows.Count;i++ )
            {
                TheLoaiDTO tl = new TheLoaiDTO();
                tl.MaTheLoai = (int)dtCapNhat.Rows[i]["MaTheLoai"];
                tl.TenTheLoai = (string)dtCapNhat.Rows[i]["TenTheLoai"];
                TheLoaiService.Update(tl);
            }
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridView();
        }
        private void LoadGridView()
        {
            dgvTheLoai.Columns.Clear();
            dgvTheLoai.AutoGenerateColumns = false;
            dgvTheLoai.Columns.Add("MaTheLoai", "Mã thể loại");
            dgvTheLoai.Columns[0].DataPropertyName = "MaTheLoai";
            dgvTheLoai.Columns.Add("TenTheLoai", "Tên thể loại");
            dgvTheLoai.Columns[1].DataPropertyName = "TenTheLoai";

            dt = TheLoaiService.GetTheLoaiTable();
            dt.TableName = "dt";
            dt.AcceptChanges();
            dv.Table = dt;
            dgvTheLoai.DataSource = dt;
            
        }

        

       

    }
}
